package net.hvacapp.cmvrfforandroid.ui.resource;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.manager.ProjectManager;
import net.hvacapp.cmvrfforandroid.ui.EventBus.NewMessageRefreshEvent;
import net.hvacapp.cmvrfforandroid.ui.EventBus.ProjectListRefreshEvent;
import net.hvacapp.cmvrfforandroid.ui.ViewDetailActivity;
import net.hvacapp.cmvrfforandroid.ui.fragment.BaseFragment;
import net.hvacapp.cmvrfforandroid.ui.mine.model.GetNoticeDataResp;
import net.hvacapp.cmvrfforandroid.ui.mine.model.NoticeData;
import net.hvacapp.cmvrfforandroid.ui.project.BrowserActivity;
import net.hvacapp.cmvrfforandroid.ui.project.ProjectStatusModifyActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetProjectInfoDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.ProjectInfoData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.utils.DateTimeUtils;
import net.hvacapp.cmvrfforandroid.utils.NumberUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


@EFragment(R.layout.fragment_notice_list)
public class NoticeListFragment extends BaseFragment {


    @ViewById
    ListView lv_project;

    private QuickAdapter<NoticeData> mQuickAdapter;

    public NoticeListFragment() {
        // Required empty public constructor
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {

        }
    }

    @AfterViews
    public void init(){

        if (mQuickAdapter == null){
            mQuickAdapter = new QuickAdapter<NoticeData>(getActivity(),R.layout.notice_cell_item) {
                @Override
                protected void convert(BaseAdapterHelper helper, NoticeData item) {
                    if (item.getViewed() == 1) {
                        helper.setVisible(R.id.iv_new, false);
                    }
                    else {
                        helper.setVisible(R.id.iv_new, true);
                    }
                    helper.setVisible(R.id.iv_navi, true);
                    helper.setText(R.id.tv_title, item.getTitle());
                    helper.setText(R.id.tv_info, item.getContent());
                    helper.setText(R.id.tv_date, DateTimeUtils.getDate(item.getTime()));
                    helper.setText(R.id.tv_from, item.getFrom());
                }
            };
        }

        lv_project.setAdapter(mQuickAdapter);

        initData();

        initListener();
    }

    private void initListener() {
        lv_project.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //查看公告，并修改可读状态
                NoticeData data = (NoticeData) parent.getItemAtPosition(position);
                readOneNotice(data.getGuidId());
                Intent intent = new Intent(getActivity(), AnnotationUtil.get(ViewDetailActivity.class));
                intent.putExtra("type", 0);
                intent.putExtra("title", data.getTitle());
                intent.putExtra("info", data.getFrom() + " " + DateTimeUtils.getDate(data.getTime()));
                intent.putExtra("content", data.getContent());
                intent.putExtra("id", data.getGuidId());
                startActivity(intent);
            }
        });
    }

    private void initData() {

        loadAllNotices(0, -1);
    }

    private void loadAllNotices(int si, int ei) {
        Map<String, Object> param = new HashMap<>();
        param.put("si", si);
        param.put("ei", ei);
        param.put("os", 0); //1: 仅返回自己发布的公告 0: 返回上级公司发布的公告

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_ALL_NOTICES, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetNoticeDataResp>() {
            @Override
            public void onSuccess(GetNoticeDataResp obj) {
                // 成功
                mQuickAdapter.clear();
                mQuickAdapter.addAll(obj.getGetPostResult());
            }

            @Override
            public void onFailure(String response) {
                showToast("获取公告失败，请重试！");
            }
        });
    }

    private void readOneNotice(String noticeId) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", noticeId);

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_VIEW_NOTICE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetNoticeDataResp>() {
            @Override
            public void onSuccess(GetNoticeDataResp obj) {
                // 成功
                EventBus.getDefault().post(new NewMessageRefreshEvent());
            }

            @Override
            public void onFailure(String response) {

            }
        });
    }

}
